home *** CD-ROM | disk | FTP | other *** search
/ Aminet 41 / Aminet 41 (2001)(Schatztruhe)[!][Feb 2001].iso / Aminet / dev / c / libmpeg_src.lha / wrapper.c < prev   
Encoding:
C/C++ Source or Header  |  1995-10-11  |  13.2 KB  |  397 lines

  1. /*
  2.  * Copyright (c) 1994 by Gregory P. Ward.
  3.  * All rights reserved.
  4.  * 
  5.  * This file is part of the MNI front end of the Berkeley MPEG decoder.
  6.  * 
  7.  * Permission to use, copy, modify, and distribute this software and its
  8.  * documentation for any purpose, without fee, and without written agreement is
  9.  * hereby granted, provided that the above copyright notice and the following
  10.  * two paragraphs appear in all copies of this software.
  11.  * 
  12.  * IN NO EVENT SHALL THE AUTHOR BE LIABLE TO ANY PARTY FOR DIRECT,
  13.  * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
  14.  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE
  15.  * UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
  16.  * SUCH DAMAGE.
  17.  *
  18.  * THE AUTHOR SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT
  19.  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  20.  * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER
  21.  * IS ON AN "AS IS" BASIS, AND THE AUTHOR HAS NO OBLIGATION TO PROVIDE
  22.  * MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.  
  23.  */
  24.  
  25. /* ----------------------------- MNI Header -----------------------------------
  26. @NAME       : wrapper.c
  27. @INPUT      : 
  28. @OUTPUT     : 
  29. @RETURNS    : 
  30. @DESCRIPTION: Functions and variables used in the interface between
  31.               user applications and the Berkely MPEG decoder.  This
  32.               file essentially comprises the MNI front end to the
  33.           Berkeley decoder; it has access to the decoder's global
  34.           variables (via globals.h), and also has a few of its own;
  35.               however, applications should NOT have access to any of 
  36.               these.  The functions in this file are the only ones to
  37.               which users of the MPEG library have access; all other
  38.               functions in the library are private.
  39. @METHOD     : 
  40. @GLOBALS    : 
  41. @CALLS      : 
  42. @CREATED    : 
  43. @MODIFIED   : 
  44. ---------------------------------------------------------------------------- */
  45.  
  46. #include <config.h>
  47. #include <stdio.h>
  48. #include <errno.h>
  49. #include "video.h"
  50. #include "proto.h"
  51. #include "util.h"
  52. #include "dither.h"
  53. #include "globals.h"
  54.  
  55. #define BUF_LENGTH 80000
  56.  
  57.  
  58. /*
  59.  * Global variables shared between this file and globals.c 
  60.  * (but nowhere else!) 
  61.  */
  62. Boolean        FrameDone;
  63. char       *CurrentImage;
  64. ImageDesc   ImageInfo;
  65. int         totNumFrames = 0;
  66.  
  67. /* Global variables used in this file only (but passed to mpegVidRsrc()): */
  68.  
  69. static VidStream *theStream;
  70.  
  71.  
  72. /* Prototypes for functions local to this file: */
  73.  
  74. void GetMPEGInfo (VidStream *vid_stream, ImageDesc *Info);
  75.  
  76.  
  77.  
  78. /* ----------------------------- MNI Header -----------------------------------
  79. @NAME       : OpenMPEG
  80. @INPUT      : MPEGfile - pointer to a stream opened for reading, positioned
  81.                          at the beginning of an MPEG stream
  82.           ImgInfo  - pointer to an ImageDesc structure which will have
  83.                      information such as frame height, width, depth
  84.              and size (total bytes per frame) put in it.
  85. @OUTPUT     : 
  86. @RETURNS    : 
  87. @DESCRIPTION: Creates and initializes the variables needed to start 
  88.               reading/decoding an MPEG stream.  
  89.  
  90.               This function is part of the MNI front end to the Berkeley
  91.           MPEG decoder, adapted from the original Berkeley code.
  92. @METHOD     : 
  93. @GLOBALS    : theStream
  94.               ditherType
  95.           input
  96.           LUM_RANGE, CR_RANGE, CB_RANGE
  97.           lum_values, cr_values, cb_values
  98. @CALLS      : GetMPEGInfo()
  99.               init_tables()
  100.               InitDither()
  101. @CREATED    : 94/6/16, Greg Ward (adapted from main() in the original
  102.               Berkeley source)
  103. @MODIFIED   : 
  104. ---------------------------------------------------------------------------- */
  105. Boolean OpenMPEG (FILE *MPEGfile, ImageDesc *ImgInfo)
  106. {
  107.    /* 
  108.     * First reinitialize these globals in case we're opening a second
  109.     * (or more) file - thanks to Loring Holding <art054@cs.brown.edu>
  110.     * for the patch.
  111.     */
  112.  
  113.    curBits = bitOffset = bufLength = 0;
  114.    EOF_flag = FALSE;
  115.  
  116.  
  117.    /* 
  118.     * Create the video stream and read the first chunk to get movie
  119.     * stats -- width and height in particular.
  120.     */
  121.  
  122.    theStream = NewVidStream(BUF_LENGTH);
  123.    if (theStream == NULL)
  124.    {
  125.       fprintf (stderr, "Error creating video stream\n");
  126.       return (FALSE);
  127.    }
  128.    
  129.    input = MPEGfile;
  130.    if (mpegVidRsrc(0, theStream) == NULL)
  131.    {
  132.       fprintf (stderr, "Error reading video stream, or stream empty\n");
  133.       return (FALSE);
  134.    }
  135.  
  136.    GetMPEGInfo (theStream, ImgInfo);
  137.  
  138.    /* Allocate/initialize tables used for dithering (?) */
  139.  
  140.    lum_values = (int *) malloc(LUM_RANGE*sizeof(int));
  141.    cr_values = (int *) malloc(CR_RANGE*sizeof(int));
  142.    cb_values = (int *) malloc(CB_RANGE*sizeof(int));
  143.  
  144.    init_tables();        /* initialize decoding stuff */
  145.  
  146.    InitDither (ImgInfo);    /* initializes dithering structures and */
  147.                 /* colormap (i.e. this is where we do */
  148.                 /* all dither-specific stuff) */
  149.    return (TRUE);
  150. }     /* OpenMPEG () */
  151.  
  152.  
  153. /* ----------------------------- MNI Header -----------------------------------
  154. @NAME       : CloseMPEG
  155. @INPUT      : (none)
  156. @OUTPUT     : (none)
  157. @RETURNS    : (void)
  158. @DESCRIPTION: Frees up some of the memory allocated by OpenMPEG() (some
  159.               is not freed because the Berkeley code doesn't take into 
  160.           account the fact that somebody might want to, say, free
  161.           up the memory it allocates... someday, I'll probably have
  162.           to hack into it to fix that, but not today thanks.)
  163. @METHOD     : 
  164. @GLOBALS    : theStream
  165.               lum_values
  166.           cr_values
  167.           cb_values
  168. @CALLS      : DestroyVidStream
  169. @CREATED    : 94/6/27, Greg Ward
  170. @MODIFIED   : 
  171. ---------------------------------------------------------------------------- */
  172. void CloseMPEG ()
  173. {
  174.    DestroyVidStream (theStream);
  175.    free (lum_values);
  176.    free (cr_values);
  177.    free (cb_values);
  178. }
  179.  
  180.  
  181.  
  182. /* ----------------------------- MNI Header -----------------------------------
  183. @NAME       : RewindMPEG
  184. @INPUT      : MPEGfile - the input stream where the MPEG's coming from
  185.               Image    - image descriptor (just passed to OpenMPEG ())
  186. @OUTPUT     : (none)
  187. @RETURNS    : (void)
  188. @DESCRIPTION: Resets things so that the caller can start reading the MPEG
  189.               stream from the start again.  Note that the caller does NOT
  190.           need to call OpenMPEG() again -- after a call to RewindMPEG(),
  191.           the next call to GetMPEGFrame() will return the first frame
  192.           of the MPEG.
  193. @METHOD     : 
  194. @GLOBALS    : EOF_flag, curBits, bitOffset, bufLength, bitBuffer, totNumFrames
  195. @CALLS      : 
  196. @CREATED    : 94/7/20, Greg Ward
  197. @MODIFIED   : 
  198. @COMMENTS   : The global variables declared in this function should
  199.               not normally be used by the front end to the MPEG
  200.               decoder.  However, I've chosen to bend the rules for
  201.               just this one function.  N.B.: EOF_flag comes from
  202.               globals.c; curBits, bitOffset, bufLength, and bitBuffer
  203.               from util.c; and totNumFrames is defined in this file.
  204. ---------------------------------------------------------------------------- */
  205. Boolean RewindMPEG (FILE *MPEGfile, ImageDesc *Image)
  206. {
  207.    CloseMPEG ();
  208.    rewind (MPEGfile);
  209.    bitBuffer = NULL;
  210.    totNumFrames = 0;
  211.  
  212.    return (OpenMPEG (MPEGfile, Image));
  213. }
  214.  
  215.  
  216.  
  217. /* ----------------------------- MNI Header -----------------------------------
  218. @NAME       : GetMPEGInfo
  219. @INPUT      : vid_stream - a video stream that is open and has had at 
  220.                            least one call to mpegVidRsrc() performed on it
  221.               Info       - a pointer to an ImageDesc struct in the caller's
  222.                        space (i.e., the argument to OpenMPEG()) where
  223.                the image information will be copied
  224. @OUTPUT     : 
  225. @RETURNS    : (void)
  226. @DESCRIPTION: From the video stream, determines the width, height, pixel
  227.               size and depth (in bits) and total image size (in bytes)
  228.           for an MPEG stream.  Sets the global variable ImageInfo
  229.           (part of the interface between wrapper.c and globals.c),
  230.           and then copies that struct to the user application's 
  231.           space via the Info pointer.
  232. @METHOD     : 
  233. @GLOBALS    : ImageInfo
  234. @CALLS      : 
  235. @CREATED    : 94/6/17, Greg Ward: based on code from ExecuteDisplay() in the
  236.               original Berkeley source
  237. @MODIFIED   : 
  238. ---------------------------------------------------------------------------- */
  239. void GetMPEGInfo (VidStream *vid_stream, ImageDesc *Info)
  240. {
  241.    switch (ditherType)
  242.    {
  243.       case Twox2_DITHER:
  244.       {
  245.      ImageInfo.Height = vid_stream->mb_height * 32;
  246.      ImageInfo.Width = vid_stream->mb_width * 32; 
  247.      ImageInfo.Depth = 8;
  248.      ImageInfo.PixelSize = 8;
  249.      ImageInfo.BitmapPad = 8;
  250.      break;
  251.       } 
  252.       case FULL_COLOR_DITHER:
  253.       {
  254.      ImageInfo.Height = vid_stream->mb_height * 16;
  255.      ImageInfo.Width = vid_stream->mb_width * 16; 
  256.      ImageInfo.Depth = 24;
  257.      ImageInfo.PixelSize = 32;
  258.      ImageInfo.BitmapPad = 32;
  259.      break;
  260.       } 
  261.       case MONO_DITHER:
  262.       case MONO_THRESHOLD:
  263.       {
  264.      ImageInfo.Height = vid_stream->mb_height * 16;
  265.      ImageInfo.Width = vid_stream->mb_width * 16; 
  266.      ImageInfo.Depth = 1;
  267.      ImageInfo.PixelSize = 1;
  268.      ImageInfo.BitmapPad = 8;
  269.      break;
  270.       } 
  271.       default:            /* including GRAY_DITHER and ORDERED_DITHER */
  272.       {
  273.      ImageInfo.Height = vid_stream->mb_height * 16;
  274.      ImageInfo.Width = vid_stream->mb_width * 16; 
  275.      ImageInfo.Depth = 8;
  276.      ImageInfo.PixelSize = 8;
  277.      ImageInfo.BitmapPad = 8;
  278.      break;
  279.       }
  280.    }     /* switch on ditherType */
  281.  
  282.    ImageInfo.Size = (ImageInfo.Height*ImageInfo.Width*ImageInfo.PixelSize) / 8;
  283.    ImageInfo.PictureRate = vid_stream->picture_rate;
  284.    ImageInfo.BitRate = vid_stream->bit_rate;
  285.  
  286.    memcpy (Info, &ImageInfo, sizeof (ImageDesc));
  287.  
  288. }     /* GetMPEGInfo () */
  289.  
  290.  
  291.  
  292. /* ----------------------------- MNI Header -----------------------------------
  293. @NAME       : SetMPEGOption
  294. @INPUT      : Option - which option to set
  295.               Value  - what to set it to
  296. @OUTPUT     : 
  297. @RETURNS    : 
  298. @DESCRIPTION: Set an MPEG option.  The options are all assigned intelligent
  299.               defaults when they are created (as global variables), so 
  300.           calling SetMPEGOption is optional (as you might expect 
  301.           from the name).  Whether SetMPEGOption() is called before 
  302.           or after OpenMPEG() is important, but it depends on which
  303.           option you're setting.  In particular, the dithering type
  304.           and luminance/chromaticity ranges must be set before
  305.           OpenMPEG(); but (unless your code is more clever than it
  306.           needs to be), the colourmap indeces probably won't be set
  307.           until after OpenMPEG().  RTFM for explanations of what the
  308.           individual options do.
  309.  
  310.           The currently available options are:
  311.                  MPEG_DITHER
  312.          MPEG_LUM_RANGE
  313.          MPEG_CR_RANGE
  314.          MPEG_CB_RANGE
  315.          MPEG_CMAP_INDEX
  316.  
  317. @METHOD     : 
  318. @GLOBALS    : Depending on the value of Option, sets one of the MPEG
  319.               decoders global variables:
  320.              ditherType
  321.          LUM_RANGE
  322.          CR_RANGE
  323.          CB_RANGE
  324. @CALLS      : 
  325. @CREATED    : 94/6/17, Greg Ward.
  326. @MODIFIED   : 95/3/18, GW: added MPEG_CMAP_INDEX option.
  327. ---------------------------------------------------------------------------- */
  328. void SetMPEGOption (MPEGOptionEnum Option, int Value)
  329. {
  330.    switch (Option)
  331.    {
  332.       case MPEG_DITHER:    ditherType = (DitherEnum) Value; break;
  333.       case MPEG_LUM_RANGE: LUM_RANGE = Value; break;
  334.       case MPEG_CR_RANGE:  CR_RANGE = Value; break;
  335.       case MPEG_CB_RANGE:  CB_RANGE = Value; break;
  336. #if (ENABLE_DITHER)
  337.       case MPEG_CMAP_INDEX:
  338.       {
  339.      int            i;
  340.      unsigned char *cmap_index;
  341.  
  342.      cmap_index = (unsigned char *) Value;
  343.      for (i = 0; i < ImageInfo.ColormapSize; i++)
  344.      {
  345.         pixel[i] = cmap_index[i];
  346.      }
  347.      break;
  348.       }
  349. #endif
  350.         
  351.    }
  352. }     /* SetMPEGOption () */
  353.  
  354.  
  355.  
  356. /* ----------------------------- MNI Header -----------------------------------
  357. @NAME       : GetMPEGFrame
  358. @INPUT      : 
  359. @OUTPUT     : Frame - the image data, converted to RGB space, is
  360.               copied to the area pointed to by Frame.  There must be
  361.               enough room for the entire image; the ImageDesc
  362.               structure returned by OpenMPEG() will tell you just how
  363.               much memory this is.  The format of the data depends on
  364.               the dithering type; for full colour dithering, there are
  365.               four bytes per pixel: red, green, blue, and unused.
  366.               (Perfect for passing to lrectwrite() or XPutImage().)
  367. @RETURNS    : TRUE if there are still frames left to decode
  368.               FALSE if we have just decoded the last frame
  369. @DESCRIPTION: Part of the MNI front end to the Berkeley MPEG decoder.  
  370.               Essentially reads, decodes, and converts to RGB space the
  371.           next frame in the MPEG stream opened with OpenMPEG().
  372. @METHOD     : 
  373. @GLOBALS    : theStream
  374.               CurrentImage
  375. @CALLS      : mpegVidRsrc ()
  376. @CREATED    : 94/6/16, Greg Ward
  377. @MODIFIED   : 
  378. ---------------------------------------------------------------------------- */
  379. Boolean GetMPEGFrame (char *Frame)
  380. {
  381.    Boolean   MovieDone = FALSE;
  382.  
  383.    FrameDone = FALSE;
  384.    dprintf ("GetMPEGFrame: starting or continuing movie\n");
  385.  
  386.    while (!MovieDone && !FrameDone) /* FrameDone is set by ExecuteDisplay() */
  387.    {
  388.       MovieDone = (mpegVidRsrc (0, theStream) == NULL);
  389.    }
  390.  
  391.    dprintf ("\nGetMPEGFrame: just received a finished frame: "
  392.         "copying from %08X to %08X\n", CurrentImage, Frame);
  393.    memcpy (Frame, CurrentImage, ImageInfo.Size);
  394.    return (!MovieDone);
  395.  
  396. }     /* GetMPEGFrame () */
  397.